package com.example.work3.service;

import com.example.work3.entity.User;
import com.example.work3.pojo.QueryAccount;
import com.example.work3.pojo.Result;
import com.example.work3.repository.UserRepository;
import com.example.work3.tools.Jjwt;
import com.example.work3.tools.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    //登录
    public Result login(String account, String password){
        String s = Md5.md5Encrypt(password);
        String first=Md5.md5Encrypt("123456");
        User user = userRepository.findByAccountAndPassword(account, s);
        if (user == null) {
            return new Result("用户名或密码错误",String.valueOf(-1));
        } else if (user.getPassword().equals(first) && user.getRole() == 0) {
            String token = Jjwt.createToken(user.getAccount(), 0);
            return new Result(token,String.valueOf(2));//提示修改密码
        }else {
            String token = Jjwt.createToken(user.getAccount(), user.getRole());
            return new Result(token,String.valueOf(user.getRole()));
        }
    }
    //修改密码
    public boolean updatePassword(String account, String password){
        String s = Md5.md5Encrypt(password);
         return userRepository.updateUserByAccount(account, s)>0;
    }
    //管理员分页查询
    public Page<User> getUsersByPageAndCondition(QueryAccount queryAccount){
        if (queryAccount==null || queryAccount.getPage()==null || queryAccount.getPageSize()==null){
            return null;
        } else {
            if((queryAccount.getSearchQuery()==null || queryAccount.getSearchQuery().isEmpty())
                    && (queryAccount.getSelectedRole()==null||queryAccount.getSelectedRole().isEmpty())){
                PageRequest pageRequest = PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                        Integer.parseInt(queryAccount.getPageSize()));
                return userRepository.findAll(pageRequest);
            }else {
                if (!queryAccount.getSearchQuery().equals("") && !queryAccount.getSelectedRole().equals("")) {
                    return userRepository.findAllByAccountAndRole(queryAccount.getSearchQuery(),
                            Integer.parseInt(queryAccount.getSelectedRole()),
                            PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                                    Integer.parseInt(queryAccount.getPageSize())));
                } else if (!queryAccount.getSearchQuery().equals("")) {
                    return userRepository.findAllByAccount(queryAccount.getSearchQuery(),
                            PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                            Integer.parseInt(queryAccount.getPageSize())));
                }else{
                    return userRepository.findAllByRole(Integer.parseInt(queryAccount.getSelectedRole()),
                            PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                            Integer.parseInt(queryAccount.getPageSize())));
                }
            }
        }
    }
    //管理员添加账号
    public User addUser(User user){
        if (user.getRole()==null){
            user.setRole(0);
        }
        String s = Md5.md5Encrypt(user.getPassword());
        user.setPassword(s);
        return userRepository.save(user);
    }
    //管理员修改账号
    public User updateUser(User user){
        return userRepository.save(user);
    }
    //管理员删除用户
    public boolean deleteUserByAccount(String account){
        return userRepository.deleteUserByAccount(account)>0;
    }
}
